package com.txby.platformApi.component.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.jck.mybatis.log.TxbyLogger;
import com.jck.mybatis.mapper.MybatisBaseDaoCRUD;
import com.txby.shopCore.entity.GoodsOrders;
import com.txby.shopOrders.component.util.QuartzFactoryUtil;
import com.txby.shopOrders.component.web.ApiOrdersController;

public class StartUpConfirmOrderJobTask implements Job{
	private static Logger logger = LoggerFactory.getLogger(StartUpConfirmOrderJobTask.class);
	public static String defaultJobName = "StartUpConfirmOrderJobTask";
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		TxbyLogger.getLogger(TxbyLogger.class).log(TxbyLogger.getTxbyLoggerLevel(), JSON.toJSONString("启动订单校验"+ApiOrdersController.taskConfirmOrderMnu+"分确认收货任务调度"+defaultJobName));
		JobDataMap dataMap = context.getJobDetail().getJobDataMap();
		MybatisBaseDaoCRUD mBaseDao=  (MybatisBaseDaoCRUD) dataMap.get("mBaseDao");
		Map map=new HashMap();
		map.put("mBaseDao", mBaseDao);
		boolean result=false;
		String  sql="SELECT   b.outTradeNo,b.paymentTime,b.payStatus,b.id_ shopsThirdPayOrderId,go.id_ ordersId,go.ordersStatus,b.collectionMethod,TIMESTAMPDIFF(MINUTE,now(),date_add(go.createTime, interval "+ApiOrdersController.taskConfirmOrderMnu+" minute)) residueMinute   from t_txby_sp_shopsThirdPayOrderRelation a,t_txby_sp_shopsThirdPayOrder b,t_txby_sp_goodsOrders go WHERE a.shopsThirdPayOrderId=b.id_  and  a.ordersId=go.id_ and  go.ordersStatus=1";
		List<Map> lst=mBaseDao.findBySql(sql, null, null);
		List<Long> tempordersId=new ArrayList<>();
		List<Long> tempshopsThirdPayOrderId=new ArrayList<>();
//		Date curDate=new Date();
		for(Map m:lst){
			Long shopsThirdPayOrderId=new Long(m.get("shopsThirdPayOrderId").toString());
			Long ordersId=new Long(m.get("ordersId").toString());
			Long payStatus=new Long(m.get("payStatus").toString());
			Long ordersStatus=new Long(m.get("ordersStatus").toString());
			String  outTradeNo=m.get("outTradeNo").toString();
			Long collectionMethod=new Long(m.get("collectionMethod").toString());
			Integer residueMinute=new Integer(m.get("residueMinute").toString());
//			Date odCreateTime=(Date) m.get("odCreateTime");
		 
			if(ordersStatus==1){
				if(residueMinute<=0){
					if(!tempordersId.contains(ordersId)){
						GoodsOrders go=new GoodsOrders();
						go.setId_(ordersId);
						go.setOrdersStatus(2);
						mBaseDao.updateObject(go);
						QuartzFactoryUtil.getInstance().removeJobByCalendar(StartUpConfirmOrderJobTask.defaultJobName+ordersId, StartUpConfirmOrderJobTask.defaultJobName+ordersId);
						QuartzFactoryUtil.getInstance().addJobByCalendar(StartUpCloseOrderJobTask.defaultJobName+ordersId,StartUpCloseOrderJobTask.defaultJobName+ordersId, StartUpCloseOrderJobTask.class,ApiOrdersController.taskCloseOrderMnu, map);
					}
					tempordersId.add(ordersId);
					
				}else{
					QuartzFactoryUtil.getInstance().addJobByCalendar(StartUpConfirmOrderJobTask.defaultJobName+ordersId,StartUpConfirmOrderJobTask.defaultJobName+ordersId, StartUpConfirmOrderJobTask.class,residueMinute, map);
				}
				
				
			}
		 
		}
		 
		QuartzFactoryUtil.getInstance().removeJobByCalendar(defaultJobName, defaultJobName);
	 
	}

	 
	
}
